package com.ltkj.system.controller;

import com.ltkj.common.core.utils.poi.ExcelUtil;
import com.ltkj.common.core.web.controller.BaseController;
import com.ltkj.common.core.web.domain.AjaxResult;
import com.ltkj.common.core.web.page.TableDataInfo;
import com.ltkj.common.log.annotation.Log;
import com.ltkj.common.log.enums.BusinessType;
import com.ltkj.common.security.annotation.RequiresPermissions;
import com.ltkj.system.api.domain.SysStores;
import com.ltkj.system.api.domain.SysUser;
import com.ltkj.system.domain.SysUserStores;
import com.ltkj.system.domain.vo.SysUserStoresVo;
import com.ltkj.system.service.ISysStoresService;
import com.ltkj.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 基础店铺信息Controller
 * 
 * @author xupf
 * @date 2023-09-09
 */
@Api(tags = "基础店铺信息")
@RestController
@RequestMapping("/stores")
public class SysStoresController extends BaseController
{
    @Autowired
    private ISysStoresService sysStoresService;

    /**
     * 根据数据权限查询基础店铺信息列表
     */
    @ApiOperation("根据数据权限查询基础店铺信息列表")
    @GetMapping("/listByDataScope")
    public TableDataInfo listByDataScope(SysStores sysStores)
    {
        List<SysStores> list = sysStoresService.selectListByDataScope(sysStores);
        return getDataTable(list);
    }

    /**
     * 查询基础店铺信息列表
     */
    @ApiOperation("查询基础店铺信息列表")
    @GetMapping("/list")
    public TableDataInfo list(SysStores sysStores)
    {
        startPage();
        List<SysStores> list = sysStoresService.selectSysStoresList(sysStores);
        return getDataTable(list);
    }
    /**
     * 查询基础店铺信息列表
     */
    @ApiOperation("查询基础店铺信息下拉列表 ")
    @GetMapping("/qurylist")
    public AjaxResult qurylist(SysStores sysStores)
    {
        List<SysStores> list = sysStoresService.selectSysStoresList(sysStores);
        return success(list);
    }
    /**
     * 导出基础店铺信息列表
     */
    @ApiOperation("导出基础店铺信息列表")
    @RequiresPermissions("system:stores:export")
    @Log(title = "基础店铺信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysStores sysStores)
    {
        List<SysStores> list = sysStoresService.selectSysStoresList(sysStores);
        ExcelUtil<SysStores> util = new ExcelUtil<SysStores>(SysStores.class);
        util.exportExcel(response, list, "基础店铺信息数据");
    }

    /**
     * 获取基础店铺信息详细信息
     */
    @ApiOperation("获取基础店铺信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysStoresService.selectSysStoresById(id));
    }

    /**
     * 新增基础店铺信息
     */
    @ApiOperation("新增基础店铺信息")
    @RequiresPermissions("system:stores:add")
    @Log(title = "基础店铺信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysStores sysStores)
    {
        if (!sysStoresService.checkStoresNameUnique(sysStores))
        {
            return error("新增店铺'" + sysStores.getName() + "'失败，店铺名称已存在");
        }
        return toAjax(sysStoresService.insertSysStores(sysStores));
    }

    /**
     * 修改基础店铺信息
     */
    @ApiOperation("修改基础店铺信息")
    @RequiresPermissions("system:stores:edit")
    @Log(title = "基础店铺信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysStores sysStores)
    {
        if (!sysStoresService.checkStoresNameUnique(sysStores))
        {
            return error("修改店铺'" + sysStores.getName() + "'失败，店铺名称已存在");
        }
        return toAjax(sysStoresService.updateSysStores(sysStores));
    }

    /**
     * 删除基础店铺信息
     */
    @ApiOperation("删除基础店铺信息")
    @RequiresPermissions("system:stores:remove")
    @Log(title = "基础店铺信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysStoresService.deleteSysStoresByIds(ids));
    }

    /**
     * 查询已分配用户角色列表
     */
    @RequiresPermissions("system:stores:list")
    @GetMapping("/authUser/allocatedList")
    public TableDataInfo allocatedList(SysUserStoresVo user)
    {
        startPage();
        List<SysUser> list = sysStoresService.selectAllocatedList(user);
        return getDataTable(list);
    }

    /**
     * 查询未分配用户店铺列表
     */
    @RequiresPermissions("system:stores:list")
    @GetMapping("/authUser/unallocatedList")
    public TableDataInfo unallocatedList(SysUserStoresVo user)
    {
        startPage();
        List<SysUser> list = sysStoresService.selectUnallocatedList(user);
        return getDataTable(list);
    }

    /**
     * 取消授权用户
     */
    @RequiresPermissions("system:stores:edit")
    @Log(title = "店铺管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancel")
    public AjaxResult cancelAuthUser(@RequestBody SysUserStores userStores)
    {
        return toAjax(sysStoresService.deleteAuthUser(userStores));
    }

    /**
     * 批量取消授权用户
     */
    @RequiresPermissions("system:stores:edit")
    @Log(title = "店铺管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/cancelAll")
    public AjaxResult cancelAuthUserAll(Long sysStoresId, Long[] userIds)
    {
        return toAjax(sysStoresService.deleteAuthUsers(sysStoresId, userIds));
    }

    /**
     * 批量选择用户授权
     */
    @RequiresPermissions("system:stores:edit")
    @Log(title = "店铺管理", businessType = BusinessType.GRANT)
    @PutMapping("/authUser/selectAll")
    public AjaxResult selectAuthUserAll(Long sysStoresId, Long[] userIds)
    {
//        sysStoresService.checkStoresDataScope(roleId);
        return toAjax(sysStoresService.insertAuthUsers(sysStoresId, userIds));
    }
}
